package com.javakc.platform.modules.circle.topic.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.javakc.platform.modules.circle.topic.entity.CircleTopic;
import com.javakc.platform.modules.circle.topic.dao.CircleTopicDao;
import com.javakc.platform.components.base.service.BaseService;

import com.javakc.platform.modules.circle.topic.vo.QueryEntity;
import com.javakc.platform.modules.system.attachment.AttachmentEnum;
import com.javakc.platform.modules.system.attachment.entity.SystemAttachment;
import com.javakc.platform.modules.system.attachment.service.SystemAttachmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author zhouhonggang
 * @version 1.0.0
 * @project rapid-development-platform
 * @datetime 2022-04-16
 * @description: [圈子话题·逻辑]
 */
@Service
public class CircleTopicService extends BaseService<CircleTopicDao, CircleTopic> {

//  注入系统-附加·逻辑层 实现类
    @Autowired
    private SystemAttachmentService systemAttachmentService;

    @Autowired
    private CircleTopicDao circleTopicDao;


    public  List<QueryEntity> queryIndexTopic(CircleTopic QueryEntity){
        return circleTopicDao.queryIndexTopic(QueryEntity);
    }




    //    重写新增方法
    @Transactional //事务 要成功都成功 要失败都失败
    public void insert(MultipartFile[] images, CircleTopic circleTopic) throws IOException {

        //      1、插入话题表   circle_topic_id
        baseMapper.insert(circleTopic);

        //      2、插入图片表  system_attachment_id
        List<SystemAttachment>list=systemAttachmentService.upload(images);

        //      3、 插入中间表

        baseMapper.insertAttachmentRelation(circleTopic.getId(),list, AttachmentEnum.TOPIC.getType());


    }


    /**
     * 根据条件分页查询
     *
     * @param entity 实体对象
     * @return 分页对象
     */
    public Page<CircleTopic> page(CircleTopic entity) {
        //封装分页参数
        Page<CircleTopic> page = Page.of(entity.getPageNo(), entity.getPageSize());
        //封装查询参数
        QueryWrapper<CircleTopic> wrapper = new QueryWrapper<>();
        //假如name有值则执行查询
        //if(StringUtils.hasLength(entity.getName())) {
        //    wrapper.like("name", entity.getName());
        //}
        //执行分页条件查询
        return baseMapper.selectPage(page, wrapper);
    }

}
